package edu.pku.sei.transformation.visitor;

import java.util.HashMap;

public class ReflectCache
{
	private HashMap<Object,Object> cacheSystem = new HashMap<Object,Object>();
	
	@SuppressWarnings("unchecked")
	public Object getCachedData(Object...args){
		if(args==null||args.length==0)
			return cacheSystem;
		
		HashMap<Object,Object> table = cacheSystem;
		
		for(int i=0;i<args.length-1;i++) {
			table = (HashMap<Object, Object>) table.get(args[i]);
			if(table==null)
				return null;
		}
				
		return table.get(args[args.length-1]);
	}
	
	@SuppressWarnings("unchecked")
	public void putCachedData(Object value,Object...args){
		if(args==null||args.length==0)
			return;
		
		HashMap<Object,Object> table = cacheSystem;
		
		for(int i=0;i<args.length-1;i++) {
			HashMap<Object,Object> ntable = (HashMap<Object, Object>) table.get(args[i]);
			if(ntable==null) {
				ntable = new HashMap<Object,Object>();
				table.put(args[i], ntable);
			}
			table = ntable;	
		}
				
		table.put(args[args.length-1],value);
	}
}
